<template>
  <div class="view-fill">
    <div v-if="!modifyInfo.visible" class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        title="省采订单详情"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button
            v-if="
              $permission('provincePurch:update') && infoList.data.status === 1
            "
            @click="update"
            >修改</el-button
          >
          <el-button v-if="canSubmit" @click="submit">提交 </el-button>
          <el-button v-if="canExcel" @click="exportExcel">导出excel</el-button>
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="infoList"> </basic-information>
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')"></Tooltip>
            <el-button
              v-if="!toSummaryFlag"
              style="margin-left: auto; margin-right: 10px"
              @click="toSummaryFlag = true"
              >汇总页</el-button
            >
            <el-button
              v-else
              style="margin-left: auto; margin-right: 10px"
              @click="toSummaryFlag = false"
              >订单页</el-button
            >
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <page-table
                v-if="!toSummaryFlag"
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :api="tableInfo.api"
                :pager="false"
                :check-box="false"
                :tab-index="true"
                :query="filterInfo.query"
                :field-list="tableInfo.fieldList"
                :isDynamic="true"
              >
                <template v-slot:col-networkUnitScale="scope">
                  {{
                    scope.row.systemUnitCoefficient +
                    scope.row.unitName +
                    '=' +
                    scope.row.networkUnitCoefficient +
                    scope.row.networkUnit
                  }}
                </template>
                <template v-slot:col-provinceAvailableQty="scope">
                  <span
                    v-if="
                      scope.row.provinceAvailableQty ||
                      scope.row.provinceAvailableQty === 0
                    "
                  >
                    {{ scope.row.provinceAvailableQty + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-sysQty="scope">
                  <span v-if="scope.row.sysQty || scope.row.sysQty === 0">
                    {{ scope.row.sysQty + scope.row.unitName }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-price="scope">
                  <span v-if="scope.row.price || scope.row.price === 0">
                    {{ scope.row.price + '元/' + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-sysPrice="scope">
                  <span v-if="scope.row.sysPrice || scope.row.sysPrice === 0">
                    {{ scope.row.sysPrice + '元/' + scope.row.unitName }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-provinceSumAmount="scope">
                  <span
                    v-if="
                      scope.row.provinceSumAmount ||
                      scope.row.provinceSumAmount === 0
                    "
                  >
                    {{ scope.row.provinceSumAmount + '元' }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-deliveryQty="scope">
                  <span
                    v-if="scope.row.deliveryQty || scope.row.deliveryQty === 0"
                  >
                    {{ scope.row.deliveryQty + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-returnQty="scope">
                  <span v-if="scope.row.returnQty || scope.row.returnQty === 0">
                    {{ scope.row.returnQty + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-receivingQty="scope">
                  <span
                    v-if="
                      scope.row.receivingQty || scope.row.receivingQty === 0
                    "
                  >
                    {{ scope.row.receivingQty + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-provinceCalcQty="scope">
                  <span
                    v-if="
                      scope.row.provinceCalcQty ||
                      scope.row.provinceCalcQty === 0
                    "
                  >
                    {{ scope.row.provinceCalcQty + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-qty="scope">
                  {{ scope.row.qty + scope.row.networkUnit }}
                </template>
              </page-table>
              <page-table
                v-else
                class="table-scroll-hidden"
                :refresh="summaryTableInfo.refresh"
                :init-curpage="summaryTableInfo.initCurpage"
                v-model:data="summaryTableInfo.data"
                :pager="false"
                :checkBox="false"
                :tab-index="true"
                :field-list="summaryTableInfo.fieldList"
                :isDynamic="true"
              >
                <template v-slot:col-qty="scope">
                  {{ scope.row.qty + scope.row.networkUnit }}
                </template>
                <template v-slot:col-networkUnitScale="scope">
                  {{
                    scope.row.systemUnitCoefficient +
                    scope.row.unitName +
                    '=' +
                    scope.row.networkUnitCoefficient +
                    scope.row.networkUnit
                  }}
                </template>
                <template v-slot:col-provinceAvailableQty="scope">
                  <span
                    v-if="
                      scope.row.provinceAvailableQty ||
                      scope.row.provinceAvailableQty === 0
                    "
                  >
                    {{ scope.row.provinceAvailableQty + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-sysQty="scope">
                  <span v-if="scope.row.sysQty || scope.row.sysQty === 0">
                    {{ scope.row.sysQty + scope.row.unitName }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-price="scope">
                  <span v-if="scope.row.price || scope.row.price === 0">
                    {{ scope.row.price + '元/' + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-sysPrice="scope">
                  <span v-if="scope.row.sysPrice || scope.row.sysPrice === 0">
                    {{ scope.row.sysPrice + '元/' + scope.row.unitName }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-provinceSumAmount="scope">
                  <span
                    v-if="
                      scope.row.provinceSumAmount ||
                      scope.row.provinceSumAmount === 0
                    "
                  >
                    {{ scope.row.provinceSumAmount + '元' }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-deliveryQty="scope">
                  <span
                    v-if="scope.row.deliveryQty || scope.row.deliveryQty === 0"
                  >
                    {{ scope.row.deliveryQty + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-returnQty="scope">
                  <span v-if="scope.row.returnQty || scope.row.returnQty === 0">
                    {{ scope.row.returnQty + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-receivingQty="scope">
                  <span
                    v-if="
                      scope.row.receivingQty || scope.row.receivingQty === 0
                    "
                  >
                    {{ scope.row.receivingQty + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-provinceCalcQty="scope">
                  <span
                    v-if="
                      scope.row.provinceCalcQty ||
                      scope.row.provinceCalcQty === 0
                    "
                  >
                    {{ scope.row.provinceCalcQty + scope.row.networkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
              </page-table>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>

    <purch-order-modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :orderId="modifyInfo.orderId"
      @returnModify="returnModify"
    />
  </div>
</template>

<script setup name="provincePurchDetail">
import { getCustomizeSetting } from '@/api/customize'
import {
  getProvincePurchOrderById,
  getProvincePurchOrderDetailCustomizeListAll,
  getProvincePurchOrderDetailExtCustomizeListAll,
  sumbitProvincePurchOrder,
  excelDetailProvincePurchOrder,
} from '@/api/province'
import PurchOrderModify from './modify'
import _ from 'lodash'

const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps(['visible', 'orderId'])
const toSummaryFlag = ref(true) //切换到汇总页：shi
const infoList = ref({
  data: {},
  fieldList: [
    { label: '采购单号', value: 'orderNo', type: 'text' },
    { label: '省采平台订单编号', value: 'provinceOrderNo', type: 'text' },
    { label: '状态', value: 'statusValue', type: 'text' },
    { label: '采购类型', value: 'purchTypeValue', type: 'text' },
    { label: '订单名称', value: 'orderName', type: 'text' },
    { label: '供应商', value: 'supplierName', type: 'text' },
    { label: '收货地址', value: 'address', type: 'popover' },
    { label: '备注', value: 'remark', type: 'popover' },
  ],
})
// 过滤相关配置
const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: null,
      },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [],
  btnList: [],
})
const summaryFilterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: null,
      },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [],
  btnList: [],
})
// 表格相关
const tableInfo = ref({
  api: undefined,
  fieldList: [],
  refresh: 1,
  initCurpage: 1,
  data: [],
})
//汇总页表格
const summaryTableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [],
})
const modifyInfo = ref({
  visible: false,
  title: '',
  orderId: '',
})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件shows
  },
})

const canSubmit = computed(() => {
  return (
    proxy.$permission('provincePurch:submit') &&
    infoList.value.data.status === 1
  )
})
const canExcel = computed(() => {
  return (
    proxy.$permission('provincePurch:excelDetail') &&
    infoList.value.data.status !== 1
  )
})
setData()
function setData() {
  let obj = {
    qty: {
      label: '数量',
      value: 'qty',
      type: 'slot',
      minWidth: '250px',
      hidden: true,
    },
    price: {
      label: '单价',
      value: 'price',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('provincePurch:price'),
      hidden: true,
    },
    orderNo: {
      label: '单号',
      value: 'orderNo',
      componentType: 'input',
      show: false,
      type: 'column',
      hidden: true,
    },
    sumAmount: {
      label: '总金额',
      value: 'sumAmount',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('provincePurch:price'),
      hidden: true,
    },
    defaultSupplierPrice: {
      label: '默认供应商价格',
      value: 'defaultSupplierPrice',
      type: 'column',
      permission: !proxy.$permission('provincePurch:price'),
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    sysPrice: {
      label: '系统单价',
      value: 'sysPrice',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('provincePurch:price'),
      hidden: true,
    },
    provinceSumAmount: {
      label: '省采金额',
      value: 'provinceSumAmount',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('provincePurch:price'),
      hidden: true,
    },
    provincePrice: {
      label: '省采单价',
      value: 'provincePrice',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('provincePurch:price'),
      hidden: true,
    },
    networkUnitScale: {
      label: 'SPD单位/省采单位',
      value: 'networkUnitScale',
      componentType: 'input',
      type: 'slot',
      hidden: true,
    },
    sysQty: {
      label: '系统采购数量',
      value: 'sysQty',
      type: 'slot',
      hidden: true,
    },
    provinceAvailableQty: {
      label: '省采可采购数量',
      value: 'provinceAvailableQty',
      type: 'slot',
      hidden: true,
    },
    deliveryQty: {
      label: '配送数量',
      value: 'deliveryQty',
      type: 'slot',
      hidden: true,
    },
    returnQty: {
      label: '退货数量',
      value: 'returnQty',
      type: 'slot',
      hidden: true,
    },
    receivingQty: {
      label: '收货数量',
      value: 'receivingQty',
      type: 'slot',
      hidden: true,
    },
    provinceCalcQty: {
      label: '省采数量',
      value: 'provinceCalcQty',
      type: 'slot',
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...obj,
  })
  summaryTableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...obj,
  })
  getCustomize()
  getCollectCustomize()
}

function returnModify() {
  getOrderInfo()
  getList()
  getCollectList()
}
function exportExcel() {
  excelDetailProvincePurchOrder(summaryFilterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function getOrderInfo() {
  getProvincePurchOrderById(props.orderId).then((resp) => {
    if (resp && resp.success) {
      infoList.value['data'] = resp.data
      if (infoList.value.data.purchType == 1) {
        tableInfo.value.fieldList.forEach((item) => {
          if (item.value === 'procureCatalogId') {
            item.hidden = true
          }
        })
        summaryTableInfo.value.fieldList.forEach((item) => {
          if (item.value === 'procureCatalogId') {
            item.hidden = true
          }
        })
      } else {
        tableInfo.value.fieldList.forEach((item) => {
          if (item.value === 'procureCatalogId') {
            item.hidden = false
          }
        })
        summaryTableInfo.value.fieldList.forEach((item) => {
          if (item.value === 'procureCatalogId') {
            item.hidden = false
          }
        })
      }
    }
  })
}
function update() {
  modifyInfo.value.visible = true
  modifyInfo.value.title = '修改省采订单'
  // 显示信息
  modifyInfo.value.orderId = props.orderId
}
function submit() {
  sumbitProvincePurchOrder([props.orderId]).then((res) => {
    if (res && res.success) {
      returnModify()
    }
  })
}

function getCustomize() {
  let _tableInfo = tableInfo.value
  let param = {
    tableKey:
      'provincePurchOrderDetailExtDetails-com.slip.spd.entity.vo.provincePlatform.ProvincePurchOrderDetailExtVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      _tableInfo.fieldList = proxy.$fn.setFieldList(
        _tableInfo.fieldList,
        data.detail
      )
      filterInfo.value.query.customizeUserVO = data
      filterInfo.value.query.queryMap.orderId.value = props.orderId
      getOrderInfo()
      getList()
    }
  })
}
function getCollectCustomize() {
  //获取汇总表格的自定义
  let param = {
    tableKey:
      'provincePurchOrderDetailCollectDetails-com.slip.spd.entity.vo.provincePlatform.ProvincePurchOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      summaryTableInfo.value.fieldList = proxy.$fn.setFieldList(
        summaryTableInfo.value.fieldList,
        data.detail
      )
      summaryFilterInfo.value.query.customizeUserVO = data
      summaryFilterInfo.value.query.queryMap.orderId.value = props.orderId
      getCollectList()
    }
  })
}
function getCollectList() {
  getProvincePurchOrderDetailCustomizeListAll(
    summaryFilterInfo.value.query
  ).then((resp) => {
    if (resp && resp.success) {
      summaryTableInfo.value.data = resp.data
    }
  })
}
function returnBack() {
  emits('returnBack', false)
}
function getList() {
  getProvincePurchOrderDetailExtCustomizeListAll(filterInfo.value.query).then(
    (resp) => {
      if (resp && resp.success) {
        tableInfo.value.data = resp.data
      }
    }
  )
}
</script>
<style scoped lang="scss">
:deep(.page-table) > .el-table {
  overflow-y: hidden;
}
.model-panel-title {
  display: flex;
  align-items: center;
  .model-tips {
    display: flex;
    align-items: center;
    margin-left: 10px;
    color: #909399;
    .model-tips-nearly,
    .model-tips-expire {
      display: block;
      margin-left: 5px;
      width: 17px;
      height: 17px;
    }
    .model-tips-nearly {
      background: #fceacb;
    }
    .model-tips-expire {
      background: #f5e4e4;
    }
  }
}
:deep(.el-table) {
  .nearly-row {
    background: #fceacb;
  }
  .expire-row {
    background: #f5e4e4;
  }
}
</style>
